<%@page import="edu.cmu.aes.project2.WarehouseService"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="javax.xml.crypto.Data"%>
<%@page import="edu.cmu.aes.project2.ws.supplier.DataElement"%>
<%@page import="edu.cmu.aes.project2.ws.supplier.BikeInfo"%>
<%@page import="edu.cmu.aes.project2.SupplierService"%>
<%
	request.setAttribute("title", "Details page");
	String errorMessage = "";
	boolean isError = false;
	String modelName = request.getParameter("modelName");
	String brandName = "";  
	if (modelName == null)
		modelName = "";
	double price = -1;
	SupplierService service = new SupplierService();
	BikeInfo bikeInfo = service.getDetails(modelName);
	if (bikeInfo == null) {
		if("".equals(modelName) )
			errorMessage = "Please go to home menu and select a model properly";
		else
		errorMessage = "Detail of model '" + modelName
				+ "' is not found";
		isError = true;
	}else {
	
		if(bikeInfo.getManufacturer()!=null){
			brandName = bikeInfo.getManufacturer();
		}
		
		price = bikeInfo.getPrice();
	}
	
	String username = UISecurityUtil.getUserName(request);
	boolean isLogin = username != null; 
	
	WarehouseService warehouseService = new WarehouseService();
	Boolean isInstock = warehouseService.instock();
	
%>
<%@include file='/WEB-INF/template/header.jsp'%>
<%@include file='/WEB-INF/template/right-menu.jsp'%>
<div id="content">
	Hello Biker!!! <br /> <br />
	<%
		if (isError) {
	%>
	<font color="red"><%=errorMessage%></font>
	<%
		} else {
	%>

	
	<% if(!isLogin){ %>
		<font id="error">You cannot purchase an item unless login to the system</font>
	<% }else if(price == -1 || price == 0){ %>
		<font id="error">You cannot purchase an item because there is no price information</font>
	<% }else if(isInstock == null){ %>
		<font id="error">Cannot connect to warehouse data. Please contact to web master</font>
	<% }else if(!isInstock){ %>
		<font id="error">You cannot purchase an item because there is no item in stock</font>
	<% }else { %>
		<script type="text/javascript">
			function buy(){
				$("#redirect_form").submit();
			}
		</script>
		<form name="redirect_form" id="redirect_form" action="purchase.jsp" method="post">
			<input type="hidden" name="brandName" id="brandName" value="<%=brandName %>">
			<input type="hidden" name="modelName" id="modelName" value="<%=modelName%>">
			<input type="hidden" name="price" id="price" value="<%=price%>">
		</form>
		<b>
			<a href="javascript:buy()"> 
			Click here to purchase this model
			</a>
		</b>
	<% } %>
	
	<br /> <br />
	<table id="detail-table">
		<tr>
			<th colspan="2"><%=modelName%></th>
		</tr>
		<tr>
			<td class="name">Year</td>
			<td class="value"><%=(bikeInfo.getYear() != null) ? bikeInfo
					.getYear() : "N/A"%></td>
		</tr>
		<tr>
			<td class="name">Brand</td>
			<td class="value"><%=(bikeInfo.getManufacturer() != null) ? bikeInfo
						.getManufacturer() : "N/A"%></td>
		</tr>
		<tr>
			<td class="name">Rating</td>
			<td class="value">
				<%
					int votes = 0;
					try {
						votes = Integer.parseInt(bikeInfo.getRatingVote());
					} catch (Exception e) {
						e.printStackTrace();
					}
					if(votes != 0)
						out.print(bikeInfo.getRatingScore()+" of 5 from "+bikeInfo.getRatingVote()+" votes");
					else 
						out.print("No votes");
				%> 
		</tr>
		<tr>
			<td class="name">Price</td>
			<td class="value">
				<%	
					DecimalFormat format = new DecimalFormat("0.00");
					if(price != -1){
						out.print("$"+format.format(price));
					}else {
						out.print("N/A");
					}
				%>
			</td>
		</tr>		
		<tr>
			<th colspan="2"><%=modelName%> Frameset</th>
		</tr>
		<% 
			DataElement[] frameSet = bikeInfo.getFrameset();
			if(frameSet == null)
				frameSet = new DataElement[0];
			for(DataElement element : frameSet){
		%>
		<tr>
			<td class="name"><%=element.getName() %></td>
			<td class="value"><%=element.getValue() %></td>
		</tr>
		<% } %>

		<tr>
			<th colspan="2"><%=modelName%> Wheels</th>
		</tr>
		<% 
			DataElement[] wheels = bikeInfo.getWheels();
		if(wheels == null)
			wheels = new DataElement[0];
			for(DataElement element : wheels){
		%>
		<tr>
			<td class="name"><%=element.getName() %></td>
			<td class="value"><%=element.getValue() %></td>
		</tr>
		<% } %>
		
		<tr>
			<th colspan="2"><%=modelName%> Drivetrain</th>
		</tr>
		<% 
			DataElement[] driveTrain = bikeInfo.getDrivetrain();
		if(driveTrain == null)
			driveTrain = new DataElement[0];
			for(DataElement element : driveTrain){
		%>
		<tr>
			<td class="name"><%=element.getName() %></td>
			<td class="value"><%=element.getValue() %></td>
		</tr>
		<% } %>
		
		<tr>
			<th colspan="2"><%=modelName%> Components </th>
		</tr>
		<% 
			DataElement[] components = bikeInfo.getComponents();
		if(components == null)
			components = new DataElement[0];
			for(DataElement element : components){
		%>
		<tr>
			<td class="name"><%=element.getName() %></td>
			<td class="value"><%=element.getValue() %></td>
		</tr>
		<% } %>
		
		<tr>
			<th colspan="2">Review </th>
		</tr>
		<tr>
			<td colspan="2"><%=(bikeInfo.getReview()==null)?"N/A":bikeInfo.getReview() %></td>
		</tr>
	</table>
	<%
		}
	%>
</div>
<%@include file='/WEB-INF/template/footer.jsp'%>
